package com.cgsd.heyu.ui.task;

import android.content.Context;
import android.content.Intent;
import android.util.TypedValue;
import android.view.View;

import androidx.core.content.ContextCompat;
import androidx.viewpager.widget.ViewPager;

import com.android.library.wedgets.TitleBarView;
import com.cgsd.heyu.R;
import com.cgsd.heyu.presenter.BasePresenter;
import com.cgsd.heyu.ui.BaseActivity;
import com.cgsd.heyu.ui.BaseFragment;
import com.cgsd.heyu.ui.task.adapter.TabAdapter;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ColorTransitionPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView;

import java.util.ArrayList;
import java.util.List;

/**
 * Author: sky on 2021/3/1 16:10
 * Email: xcode126@126.com
 * Desc: 我的任务
 */
public class MyTaskActivity extends BaseActivity<BasePresenter> {

    private MagicIndicator mIndicator;
    private TitleBarView mTitleBarView;
    private ViewPager mViewPager;

    private TabAdapter adapter;
    private String[] titles;

    public static Intent getIntent(Context context) {
        return getIntent(context, null);
    }

    public static Intent getIntent(Context context, String type) {
        Intent intent = new Intent(context, MyTaskActivity.class);
        intent.putExtra("type", type);
        return intent;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.common_indicator;
    }

    @Override
    protected void initView() {
        mTitleBarView = findViewById(R.id.title_bar);
        mViewPager = findViewById(R.id.mViewPager);
        mIndicator = findViewById(R.id.mIndicator);
        mTitleBarView.setTitle(getString(R.string.a_my_task));
        findViewById(R.id.v_line).setVisibility(View.VISIBLE);

        titles = getResources().getStringArray(R.array.a_my_task_title);
        adapter = new TabAdapter(getSupportFragmentManager(), getFragments());
        mViewPager.setAdapter(adapter);
        mViewPager.setOffscreenPageLimit(titles.length);

        initTitleIndicator();
    }

    @Override
    protected void initData() {

    }

    /**
     * 初始化菜单栏
     */
    private void initTitleIndicator() {
        CommonNavigator titleNavigator = new CommonNavigator(this);
        titleNavigator.setAdjustMode(true);//true 自适应居中、false 反之
        titleNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return titles.length;
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                SimplePagerTitleView simplePagerTitleView = new ColorTransitionPagerTitleView(context);
                simplePagerTitleView.setText(titles[index]);
                simplePagerTitleView.setPadding(0, 0, 0, 0);
                simplePagerTitleView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
                simplePagerTitleView.setNormalColor(ContextCompat.getColor(MyTaskActivity.this, R.color.textMain));
                simplePagerTitleView.setSelectedColor(ContextCompat.getColor(MyTaskActivity.this, R.color.app_color));
                simplePagerTitleView.setText(titles[index]);

                simplePagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mViewPager.setCurrentItem(index);
                    }
                });
                return simplePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator linePagerIndicator = new LinePagerIndicator(context);
                linePagerIndicator.setMode(LinePagerIndicator.MODE_WRAP_CONTENT);
                linePagerIndicator.setColors(ContextCompat.getColor(MyTaskActivity.this, R.color.app_color));
                return linePagerIndicator;
            }
        });
        mIndicator.setNavigator(titleNavigator);
        ViewPagerHelper.bind(mIndicator, mViewPager);
    }

    /**
     * get fragments
     *
     * @return
     */
    private List<BaseFragment> getFragments() {
        List<BaseFragment> list = new ArrayList<>();
        list.add(MyBookFragment.newInstance());
        list.add(MyPublishFragment.newInstance());
        return list;
    }
}
